package gamagora.game
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	
	import flash.text.engine.GroupElement;
	
	import gamagora.box2d.OSPhysicBox;
	import gamagora.box2d.OSPhysicCircle;
	import gamagora.box2d.OSPhysicData;
	import gamagora.box2d.OSPhysicSprite;
	import gamagora.box2d.OSPhysicWorld;
	import gamagora.game.map.OSWorldMap;
	import gamagora.game.utils.OSImages;
	import gamagora.game.utils.OSSounds;
	
	import org.flixel.FlxEmitter;
	import org.flixel.FlxGroup;
	import org.flixel.FlxParticle;
	import org.flixel.FlxSprite;
	
	public class OSStall extends OSPhysicBox
	{
		private static const PLANK_RADIUS:Number = 8;
		
		private var _coinLeft:OSPhysicBox;
		private var _coinRight:OSPhysicBox;
		private var _circles:Vector.<OSPhysicCircle>;
		private var _positions:Vector.<b2Vec2>;
		private var _angles:Vector.<Number>;
		private var _emitter:FlxEmitter;
		private var _nbCircles:uint;
		private var _created:Boolean;
		private var _crash:Boolean;
		
		public function OSStall(x:Number=0, y:Number=0, h:Number=20, nbRow:uint=4, nbParticles:uint=10)
		{
			var w:Number = nbRow * 2 * PLANK_RADIUS + 14;
			_crash = false;

			// box
			super(x, y, w, h, 0, 0, OSImages.STALL);
			setData(OSPhysicData.NOTHING);
			
			// coins
			_coinLeft = new OSPhysicBox(x, y-10, 1, 10);
			_coinLeft.makeGraphic(1, 10, 0xff000000);
			_coinLeft.setData(OSPhysicData.NOTHING);
			_coinRight = new OSPhysicBox(x+w-3, y-10, 1, 10);
			_coinRight.makeGraphic(1, 10, 0xff000000);
			_coinRight.setData(OSPhysicData.NOTHING);
			_positions = new Vector.<b2Vec2>();
			_angles = new Vector.<Number>();
			
			// circles
			_circles = new Vector.<OSPhysicCircle>();
			var cpt:uint = nbRow;
			
			for(var i:uint = 0; i < nbRow; ++i)
			{
				var xTmp:Number = x + 7 + PLANK_RADIUS * i;
				var yTmp:Number = y - 7 - PLANK_RADIUS - PLANK_RADIUS * 2 * i;
				
				for(var j:uint = 0; j < cpt; ++j)
				{
					var circle:OSPhysicCircle = new OSPhysicCircle(xTmp, yTmp, PLANK_RADIUS, OSImages.APPLE);
					circle.setData(OSPhysicData.FRUIT);
					circle.getPhysic().bodyDef.type = b2Body.b2_dynamicBody;
					
					circle.getPhysic().fixDef.friction = 0.9;
					circle.getPhysic().fixDef.density = 1;			
					_circles.push(circle);
					
					xTmp = xTmp + PLANK_RADIUS * 2;
				}
				--cpt;
			}
			
			_nbCircles = _circles.length;
			_nbSprites = _nbCircles + 3 + nbParticles;
			
			// Fragments
			_emitter = new FlxEmitter(x+w/2, y, nbParticles);
			_emitter.setXSpeed(-50, 50);
			_emitter.setYSpeed(-50, 50);
			_emitter.gravity = 50;
		}
		
		public override function create(group:FlxGroup):void
		{
			_inGame = true;
			if(!_created)
			{
				super.create(group);
				_coinLeft.create(group);
				_coinRight.create(group);
				
				for(var i:uint=0; i<_nbCircles; ++i)
				{
					_circles[i].create(group);
					_positions.push(new b2Vec2(_circles[i].getBody().GetPosition().x, _circles[i].getBody().GetPosition().y));
					_angles.push(_circles[i].getBody().GetAngle());
				}
				
				var particle:FlxParticle;
				for(i = 0; i < _emitter.maxSize; ++i)
				{			
					particle = new FlxParticle();
					particle.makeGraphic(30, 6, 0xff663300);
					particle.visible = true;
					_emitter.add(particle);
				}
				
				_created = true;
			}
			else 
			{
				for(i=0; i < _nbCircles; ++i)
				{
					_circles[i].create(group);
					_circles[i].getBody().SetPosition(_positions[i]);
					_circles[i].getBody().SetAngle(_angles[i]);
				}
			}
		}
		
		public override function update():void
		{
			if(_inGame)
			{
				if(!_crash)
				{
					if(OSWorldMap.player.y >= this.y-100 && OSWorldMap.player.x >= this.x - 60)
					{
						/************************/
						var tmp:OSSounds = new OSSounds();
						tmp.playWoodCrack1();
						tmp.playFruits();
						/************************/
						_emitter.start(true, 2);
						

						OSPhysicWorld.destroy(this.getBody());
						OSPhysicWorld.destroy(_coinLeft.getBody());
						OSPhysicWorld.destroy(_coinRight.getBody());
						
						_crash = true;
					}
					else 
					{
						super.update();
					}
				}
				else
				{
					_emitter.update();
				}
				
				for(var i:uint = 0; i < _nbCircles; ++i)
				{
					_circles[i].update();
				}
			}
		}
		
		public override function draw():void
		{
			if(_inGame)
			{
				for(var i:uint = 0; i < _nbCircles; ++i)
				{
					_circles[i].draw();
				}
				
				if(!_crash)
				{
					super.draw();
				}
				else 
				{
					_emitter.draw();
				}
			}
		}
		
		//Override
		
		public override function destroyPhysic():void
		{
			_inGame = false;
			
			for(var i:uint=0;i<_nbCircles;i++)
			{
				_positions[i] = new b2Vec2(_circles[i].getBody().GetPosition().x, _circles[i].getBody().GetPosition().y);
				_angles[i] = _circles[i].getBody().GetAngle();
				_circles[i].destroyPhysic();
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			for(var i:uint=0;i<_nbCircles;i++)
			{
				_circles[i].destroy();
				//OSPhysicWorld.destroy(_circles[i].getBody());
			}
			_emitter.destroy();
		}

	}
}
